/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CCPVSimulationPackage;

import SocialMatrixPackage.SocialMatrix;
import ccpv.Person;
import java.util.List;
import java.util.Vector;
import multiarray.MultiArray;
import multiarray.RecursiveAccess;

/**
 *
 * @author Heather
 */
public class CCPVSimulationPopulation
{
  public MultiArray<Person> population;
  public SocialMatrix sm;
  public MultiArray<Float> preConflictPowers;

  public CCPVSimulationPopulation()
  {    
  }

  public CCPVSimulationPopulation(SocialMatrix sm)
  {
    this.sm = sm;
  }

  public CCPVSimulationPopulation(SocialMatrix sm, MultiArray<Person> population)
  {
    this.population = population;
    this.sm = sm;
    this.preConflictPowers = getPowerFromPersonsAsFloats();
  }

  public MultiArray<Float> getPreConflictPowersAsFloats()
  {
    List<int[]> allCombos = RecursiveAccess.generateAllPermutations(sm.getDimensions());
    for (int[] i : allCombos)
    {
      System.out.println("power$$$: " + preConflictPowers.getElementAtIndex(i));
    }

    return preConflictPowers;
  }

  public MultiArray<MultiArray<Double>> getAltruismFromPersonsAsDoubles()
  {
    MultiArray<MultiArray<Double>> ret = new MultiArray<MultiArray<Double>>(sm.getDimensions());
    List<int[]> allCombos = RecursiveAccess.generateAllPermutations(sm.getDimensions());

    for (int[] source : allCombos)
    {
      Person sourceP = population.getElementAtIndex(source);
      MultiArray<Double> feelings = new MultiArray<Double>(sm.getDimensions());
      for (int[] target : allCombos)
      {
        feelings.setElementAtIndex(target, sourceP.getAltruism(target));
      }
      ret.setElementAtIndex(source, feelings);
    }

    return ret;
  }

  public MultiArray<MultiArray<Float>> getAltruismFromPersonsAsFloats()
  {
    MultiArray<MultiArray<Float>> ret = new MultiArray<MultiArray<Float>>(sm.getDimensions());
    List<int[]> allCombos = RecursiveAccess.generateAllPermutations(sm.getDimensions());

    for (int[] source : allCombos)
    {
      Person sourceP = population.getElementAtIndex(source);
      MultiArray<Float> feelings = new MultiArray<Float>(sm.getDimensions());
      for (int[] target : allCombos)
      {
        feelings.setElementAtIndex(target, (float)sourceP.getAltruism(target));
      }
      ret.setElementAtIndex(source, feelings);
    }

    return ret;
  }

  public MultiArray<Float> getPowerFromPersonsAsFloats()
  {
    Vector<int[]> allCombos = sm.getAllCombinations();

    MultiArray<Float> ret = new MultiArray<Float>(sm.getDimensions());
    for (int[] i : allCombos)
    {
      Person p = population.getElementAtIndex(i);
      if (p != null)
      {
        ret.setElementAtIndex(i, (float) p.getPower());
      }
      else
      {
        ret.setElementAtIndex(i, 0f);
      }
    }

    return ret;
  }

  public MultiArray<Double> getPowerFromPersonsAsDoubles()
  {
    Vector<int[]> allCombos = sm.getAllCombinations();

    MultiArray<Double> ret = new MultiArray<Double>(sm.getDimensions());
    for (int[] i : allCombos)
    {
      Person p = population.getElementAtIndex(i);
      if (p != null)
      {
        ret.setElementAtIndex(i, p.getPower());
      }
      else
      {
        ret.setElementAtIndex(i, 0.0);
      }
    }

    return ret;
  }

  public MultiArray<Float> getPopulationFromPersonsAsFloats()
  {
    Vector<int[]> allCombos = sm.getAllCombinations();

    MultiArray<Float> ret = new MultiArray<Float>(sm.getDimensions());
    for (int[] i : allCombos)
    {
      Person p = population.getElementAtIndex(i);
      if (p != null)
      {
        ret.setElementAtIndex(i, (float) p.getQty());
      }
      else
      {
        ret.setElementAtIndex(i, 0f);
      }
    }

    return ret;
  }

  public MultiArray<Integer> getPopulationFromPersonsAsInt()
  {
    Vector<int[]> allCombos = sm.getAllCombinations();

    MultiArray<Integer> ret = new MultiArray<Integer>(sm.getDimensions());
    for (int[] i : allCombos)
    {
      Person p = population.getElementAtIndex(i);
      ret.setElementAtIndex(i, p.getQty());
    }

    return ret;
  }

  public void updateAltruisms(MultiArray<MultiArray<Double>> newAltruisms)
  {
    updateSM(newAltruisms);
    updatePeople(newAltruisms);
  }

  public void updateSM(MultiArray<MultiArray<Double>> newAltruisms)
  {
    Vector<int[]> allCombos = sm.getAllCombinations();

    for (int[] source : allCombos)
    {
      for (int[] target : allCombos)
      {
        sm.setAltruism(source, target, newAltruisms.getElementAtIndex(source).getElementAtIndex(target));
        System.out.println("the new altruism: " + newAltruisms.getElementAtIndex(source).getElementAtIndex(target));
      }
    }
  }

  public void updatePeople(MultiArray<MultiArray<Double>> newAltruisms)
  {
    int[] dim = newAltruisms.getDimensions();
    for (Object o1 : population.objects)
    {
      Person p1 = (Person) o1;

      p1.setAltruism(newAltruisms.getElementAtIndex(p1.getCharacter()), dim);
    }
  }

  public void setSMAltruisms(MultiArray<Person> personPopulation)
  {
    /*
    Vector<int[]> allCombos = sm.getAllCombinations();
    for (int[] i : allCombos)
    {
    if(personPopulation.getElementAtIndex(i) == null)
    System.out.println("NULL FOUND!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    else
    {
    String p = "";
    for (int j = 0; j < i.length; j++)
    {
    p += j + " ";
    }
    System.out.println(p);
    System.out.println("no null");
    System.out.println("");
    }
    }*/

    /*
    for (Object o1 : personPopulation.objects)
    {
    Person p1 = (Person) o1;

    for (Object o2 : personPopulation.objects)
    {
    Person p2 = (Person) o2;
    if ((p1 != null) && (p2 != null))
    {
    double altruism = p1.getAltruism(p2.getCharacter());
    sm.setAltruism(p1.getCharacter(), p2.getCharacter(), altruism);
    }
    }
    }*/

    Vector<int[]> allCombos = sm.getAllCombinations();
    for (int[] source : allCombos)
    {
      Person p1 = personPopulation.getElementAtIndex(source);

      for (int[] target : allCombos)
      {
        Person p2 = personPopulation.getElementAtIndex(target);

        if ((p1 != null) && (p2 != null))
        {
          double value = p1.getAltruism(target);
          sm.setAltruism(source, target, value);
        }
        else
        {
          double value = 0;
          sm.setAltruism(source, target, value);
        }
      }
    }
  }

}
